package com.yanqu.road.server.cmd.wingroom.shareevent;

import com.yanqu.road.entity.player.ReturnData;
import com.yanqu.road.entity.wingroom.UserWingRoomShareEvent;
import com.yanqu.road.pb.wingroom.WingRoomProto;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.command.Command;
import com.yanqu.road.server.manger.wingroom.WingRoomMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.pb.WingRoomPb;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameProtocol;
import io.netty.channel.ChannelHandlerContext;

/**
 * @author ：zfr
 * @date ：2022/2/11 11:17
 */
@Cmd(code = GameProtocol.S_WING_ROOM_GET_EVENT_REWARD_CROSS_REQ, desc = "跨服来领取厢房奖励")
public class GetEventRewardCrossReqCmd implements Command {
    @Override
    public void execute(ChannelHandlerContext ctx, YanQuMessage packet) throws Exception {
        WingRoomProto.GetOtherPlayerEventRewardCrossForwardMsg forwardMsg = WingRoomProto.GetOtherPlayerEventRewardCrossForwardMsg.parseFrom(packet.getBytes());
        ReturnData returnData = WingRoomMgr.getOtherPlayerEventReward(forwardMsg.getOriPlayerId(), forwardMsg.getPlayerName(), forwardMsg.getOtherPlayerId(), forwardMsg.getPos());
        WingRoomProto.GetOtherPlayerEventRewardRespCrossMsg.Builder respCrossMsg = WingRoomProto.GetOtherPlayerEventRewardRespCrossMsg.newBuilder();

        respCrossMsg.setOriServerId(forwardMsg.getOriServerId());
        respCrossMsg.setOriPlayerId(forwardMsg.getOriPlayerId());

        WingRoomProto.GetOtherPlayerEventRewardRespMsg.Builder respMsg = WingRoomProto.GetOtherPlayerEventRewardRespMsg.newBuilder();
        respMsg.setRet(returnData.getRet());
        if (returnData.getRet() == 0) {
            respMsg.setReward(returnData.getParams().get(0));
        }
        UserWingRoomShareEvent userEvent = WingRoomMgr.getUserEvent(forwardMsg.getOtherPlayerId());
        if (userEvent != null) {
            respMsg.setEvent(WingRoomPb.buildEventTempMsg(userEvent));
            respMsg.setHostUserId(userEvent.getUserId());
        }
        respCrossMsg.setResp(respMsg);
        YanQuMessage message = YanQuMessageUtils.buildMessage(CrossProtocol.C_WING_ROOM_GET_OTHER_PLAYER_EVENT_REWARD_RESP, respCrossMsg);
        GamePlayerMgr.sendPacket(0, message);
    }
}
